package com.sxpro;
import java.util.Timer;
import java.util.TimerTask;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

// 定义状态接口
interface State {
    void handleKeyStroke(ControlPanel panel, char key);
    void reset(ControlPanel panel);
}

// 读取键入字符状态
class ReadKeyState implements State {
    private int charCount = 0;

    @Override
    public void handleKeyStroke(ControlPanel panel, char key) {
        if (Character.isDigit(key)) {
            panel.setPasswordChar(key);
            charCount++;
            if (charCount == 4) {
                String inputPassword = panel.getPassword();
                if (inputPassword.equals(panel.getCorrectPassword())) {
                    panel.setState(new FunctionSelectionState());
                } else {
                    panel.setErrorCount(panel.getErrorCount() + 1);
                    if (panel.getErrorCount() >= 3) {
                        panel.setState(new LockedState());
                        panel.startLockTimer();
                    } else {
                        panel.resetPassword();
                        panel.setState(new ReadKeyState());
                    }
                }
            }
        } else {
            System.out.println("输入字符无效");
        }
    }

    @Override
    public void reset(ControlPanel panel) {
        panel.resetPassword();
        charCount = 0;
    }
}

// 系统功能选择状态
class FunctionSelectionState implements State {
    @Override
    public void handleKeyStroke(ControlPanel panel, char key) {
        // 处理系统功能选择
        System.out.println("根据按键选择了系统功能: " + key);
        // Reset for next operation
        panel.setState(new ReadKeyState());
    }

    @Override
    public void reset(ControlPanel panel) {
        // No operation needed
    }
}

// 锁定状态
class LockedState implements State {
    @Override
    public void handleKeyStroke(ControlPanel panel, char key) {
        if (panel.isLockTimerExpired()) {
            panel.resetErrorCount();
            panel.stopLockTimer();
            panel.setState(new ReadKeyState());
        } else {
            System.out.println("控制面板被锁定，请稍后再试。");
        }
    }

    @Override
    public void reset(ControlPanel panel) {
        // No operation needed
    }
}

// 控制面板类
class ControlPanel {
    private State currentState;
    private String password = "";
    private String correctPassword = "1234"; // 预设密码
    private int errorCount = 0;
    private Timer lockTimer;
    private long lockTimeStart;
    private static final long LOCK_DURATION = 120000; // 120秒

    public ControlPanel() {
        currentState = new ReadKeyState();
    }

    public void setState(State state) {
        currentState = state;
    }

    public void setPasswordChar(char key) {
        password += key;
    }

    public String getPassword() {
        return password;
    }

    public String getCorrectPassword() {
        return correctPassword;
    }

    public int getErrorCount() {
        return errorCount;
    }

    public void setErrorCount(int errorCount) {
        this.errorCount = errorCount;
    }

    public void resetPassword() {
        password = "";
    }

    public void resetErrorCount() {
        errorCount = 0;
    }

    public void startLockTimer() {
        lockTimeStart = System.currentTimeMillis();
        lockTimer = new Timer();
        lockTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (System.currentTimeMillis() - lockTimeStart >= LOCK_DURATION) {
                    isLockTimerExpired();
                }
            }
        }, LOCK_DURATION);
    }

    public void stopLockTimer() {
        if (lockTimer != null) {
            lockTimer.cancel();
        }
    }

    public boolean isLockTimerExpired() {
        if (System.currentTimeMillis() - lockTimeStart >= LOCK_DURATION) {
            System.out.println("锁定时间已过");
            return true;
        }
        return false;
    }

    public void handleKeyStroke(char key) {
        currentState.handleKeyStroke(this, key);
    }

    public void reset() {
        currentState.reset(this);
    }
}